package com.sun.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.x.protobuf.MysqlxExpect;
import com.sun.mybatisplus.common.Condition;
import com.sun.mybatisplus.common.Query;
import com.sun.mybatisplus.entity.BeautyInfo;
import com.sun.mybatisplus.log.annotation.SysLog;
import com.sun.mybatisplus.redis.config.RedisCacheTemplate;
import com.sun.mybatisplus.service.BeautyService;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * 女士相关接口
 */
@RestController
@RequestMapping("/beauty")
@Api(value = "女士相关接口")
public class BeautyController {

    @Autowired
    private BeautyService beautyService;
    @Autowired
    private RedissonClient redisson;
    @Autowired
    private RedisCacheTemplate redisCacheTemplate;

    private static final String LOCK = "lock";


    /**
     * 自定义分页查询条件
     * @return
     */
    @PostMapping("/list")
    public IPage<BeautyInfo> getBeautyByPage(BeautyInfo beautyInfo, Query query){
       IPage<BeautyInfo>  beautyInfoIPage = beautyService.selectBeautyByPage(Condition.getPage(query),beautyInfo);
       return beautyInfoIPage;

    }



    /**
     * 查询所有
     *  使用链路跟踪
     * @return
     */
    @RequestMapping("/selectAllTrace")
    public List<BeautyInfo> selectAllTrace() {
        return beautyService.selectAllTrace();
    }




    /**
     * 查询所有
     *
     * @return
     */
    @RequestMapping("/selectAll")
    public List<BeautyInfo> selectAll() {
        return beautyService.list();
    }

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping("/page")
    @SysLog("查询女士分页信息")
    public IPage<BeautyInfo> selectByPage() {
        IPage<BeautyInfo> iPage = new Page<>(1, 3);
        QueryWrapper<BeautyInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("name", "柳岩");
        IPage<BeautyInfo> infoIPage = beautyService.page(iPage, wrapper);
        redisCacheTemplate.setCacheObject("beauty",infoIPage,20, TimeUnit.SECONDS);
        RLock lock = redisson.getLock(LOCK);
        //加锁
        lock.lock();
        //执行业务逻辑
        //释放锁
        lock.unlock();
        return infoIPage;
    }

    /**
     * 逻辑删除
     *
     * @return
     */
    @RequestMapping("/delete")
    public Boolean logicDelete() {
        return beautyService.removeById("10");
    }


    /**
     * 更新测试乐观锁
     *
     * @return
     */
    @RequestMapping("/update")
    public void updateInfo() {
        //线程1
        BeautyInfo beautyInfo1 = beautyService.getById(8);
        //线程2
        BeautyInfo beautyInfo2 = beautyService.getById(8);
        beautyInfo1.setNameInfo("小昭111");
        beautyInfo2.setNameInfo("小昭222");

        beautyService.updateById(beautyInfo1);
        beautyService.updateById(beautyInfo2);

    }
}
